package com.origin.utils.permissions

import android.Manifest
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentActivity
import com.origin.utils.log.LogUtils
import java.util.ArrayList

object PermissionsUtils {

    /**
     * 0 该权限已被用户选择了不再询问！
     * 1 权限未被授予！
     * 2 权限已被授予！
     */
    fun checkPermissions(activity: Activity, vararg permission: String): Boolean {

        permission.forEach {
            if (ContextCompat.checkSelfPermission(
                    activity,
                    it
                ) == PackageManager.PERMISSION_DENIED
            ) {
                return false;

            }
        }

        return true

    }

    fun isDeclinedAndNotAsked(activity: Activity, permission: MutableList<String>): Boolean {

        permission.forEach {
            if (ContextCompat.checkSelfPermission(
                    activity,
                    it
                ) == PackageManager.PERMISSION_DENIED
            ) {
                if (!ActivityCompat.shouldShowRequestPermissionRationale(activity, it)) {
                    return true;
                }

            }
        }

        return false

    }

    fun getGrantedPermissions(
        activity: Activity,
        permission: MutableList<String>
    ): MutableList<String> {
        val mutableList = mutableListOf<String>()
        permission.forEach {
            if (ContextCompat.checkSelfPermission(
                    activity,
                    it
                ) == PackageManager.PERMISSION_GRANTED
            ) {

                mutableList.add(it)


            }
        }

        return mutableList

    }

    fun getDeniedPermissions(
        activity: Activity,
        permission: MutableList<String>
    ): MutableList<String> {
        val mutableList = mutableListOf<String>()
        permission.forEach {
            if (ContextCompat.checkSelfPermission(
                    activity,
                    it
                ) == PackageManager.PERMISSION_DENIED
            ) {

                mutableList.add(it)


            }
        }

        return mutableList

    }


    fun requestPermissions(
        activity: FragmentActivity,
        vararg permissions: String,
        onPermissionPageCallback: OnPermissionPageCallback
    ) {

        PermissionsAndActivityResultFragment
            .permission(activity, permissions.toMutableList() as ArrayList<String>)
            .requestPermissions(onPermissionPageCallback)
    }

    fun simpleRequestPermissions(
        activity: FragmentActivity,
        vararg permission: String,
        onPermissionPageCallback: OnPermissionPageCallback
    ) {
        LogUtils.json("simpleRequestPermissions A：" + checkPermissions(activity, *permission))
        if (!checkPermissions(activity, *permission)) {
            requestPermissions(
                activity,
                permissions = permission,
                onPermissionPageCallback = onPermissionPageCallback
            );
            return
        }
        LogUtils.json("simpleRequestPermissions B：")
        onPermissionPageCallback.onGranted(permission.toMutableList() as ArrayList<String>)
    }

    fun startActivityForResult(
        activity: FragmentActivity,
        intent: Intent,
        onActivityResultCallback: OnActivityResultCallback,
        requestCode: Int = -1
    ) {
        val permissionPageCallback = PermissionsAndActivityResultFragment
            .forResult(activity);
        if (requestCode != -1) {
            permissionPageCallback.setRequestCode(requestCode)
        }
        permissionPageCallback.startActivityForResult(intent, onActivityResultCallback)
    }


}